// MegaCli -AdpAllInfo -aN|-a0,1,2|-aALL
// Product Name、Serial No、BIOS Version、FW Version、BBU
// if BBU's value is Absent set false, if BBU's value is Present set true

package megacli

import (
    "fmt"
    "github.com/prometheus/common/log"
    "strings"
)

// get Adapter Info
func GetAdpInfo(c *Controller) error {
    res, err := Run(clipath, "-AdpAllInfo", fmt.Sprintf("-a%d", c.Number), "-NoLog")
    if err != nil {
        return err
    }

    name, sn, bios, fw, bbu, err := parseAdpInfo(res)
    if err != nil {
        return err
    }

    c.ProductName = name
    c.SerialNumber = sn
    c.BIOS = bios
    c.Firmware = fw
    c.Battery = bbu
    return nil
}

// 解析Adapter Info
func parseAdpInfo(in string) (name, sn, bios, fw string, bbu bool, err error) {
    for _, line := range strings.Split(in, "\n") {
        key, val, err := SplitKeyVal(line, ":")
        if err != nil {
            log.Debug(err)
            continue
        }

        switch key {
        case "Product Name":
            name = val
        case "Serial No":
            sn = val
        case "BIOS Version":
            bios = val
        case "FW Version":
            fw = val
        case "BBU":
            if strings.ToLower(val) == "present" {
                bbu = true
            }
        }

        if key == "Current Time" {
            break
        }
    }

    return
}
